#include <stdio.h>
#include <Elementary.h>
#include <libcal-character.h>
#include <libui-char-status.h>
#include <libabs-appfwk.h>
#include <Edje.h>
#include <Evas.h>
#include <Ecore.h>
#include <Edje_Edit.h>

typedef struct appdata{
	Evas_Object* win_main;
	Evas_Object* layout_main;
	Evas* evas;
	void* char_db_object;
	Evas_Object* li;
	Evas_Object* status;
	Evas_Object* toolbar;
	Evas_Object* button;
	Evas_Object* bt1;
	Evas_Object* bt2;


	int id;
} appdata;

typedef struct message{
	int index;
	void *ad;
} message;


void bt_ok(void* data, Evas_Object* obj, void* event_info){
	
}
void bt_cancel(void* data, Evas_Object* obj, void* event_info){
	appdata* ad = (appdata*) data;

	Evas_Object* edje = elm_layout_edje_get(ad->layout_main);

	evas_object_hide(ad->status);
	evas_object_del(ad->status);
	evas_object_hide(ad->toolbar);
	evas_object_del(ad->toolbar);

	evas_object_show(ad->li);
	edje_object_part_swallow(edje, "list", ad->li);
	evas_object_hide(ad->bt1);
	evas_object_hide(ad->bt2);

	evas_object_hide(ad->button);

}

static void
win_del(void *data, Evas_Object *obj, void *event_info){
	libabs_appfwk_stop_loop();
}

Evas_Object* load_edj(Evas_Object* win_obj, char* filepath, char* groupname){
	Evas_Object* ret;
	int w, h;

	ret=elm_layout_add(win_obj);
	elm_layout_file_set(ret, filepath, groupname);
	evas_object_move(ret, 0, 0);
	elm_win_resize_object_add(win_obj, ret);
	evas_object_show(ret);
	return ret;
}

static void tb_cb_status(void *data, Evas_Object *obj, void* event_info){
	printf("tb_cb_status\n");
}

static void tb_cb_spirit(void *data, Evas_Object *obj, void* event_info){
	printf("tb_cb_spriti\n");
}

static void tb_cb_history(void *data, Evas_Object *obj, void* event_info){
	printf("tb_cb_history\n");
}

void clicked(void *data, Evas_Object *obj, void *event_info){
	message* mes = (message*) data;
	appdata* ad = (appdata*) mes->ad;

	Evas_Object* edje = elm_layout_edje_get(ad->layout_main);
	evas_object_hide(ad->li);

	ad->status = ui_char_status_create(ad->win_main, mes->index);
	edje_object_part_swallow(edje, "list", ad->status);

	evas_object_show(ad->status);


	Elm_Toolbar_Item *item;
	ad->toolbar = elm_toolbar_add(ad->win_main);
	elm_toolbar_homogenous_set(ad->toolbar, 0);
	evas_object_size_hint_weight_set(ad->toolbar, 0.0, 0.0);
	evas_object_size_hint_align_set(ad->toolbar, EVAS_HINT_FILL, 0.0);
	Evas_Object* ic;
	ic = elm_icon_add(ad->win_main);
	elm_icon_file_set(ic, "/usr/local/share/elementary/images/logo_small.png", NULL);

	elm_toolbar_item_add(ad->toolbar, NULL, "status", tb_cb_status, ad);
	elm_toolbar_item_add(ad->toolbar, NULL, "spirit", tb_cb_spirit, ad);
	elm_toolbar_item_add(ad->toolbar, NULL, "history", tb_cb_history, ad);

	elm_toolbar_menu_parent_set(ad->toolbar, ad->win_main);

	evas_object_resize(ad->toolbar, 300, 50);
	evas_object_show(ad->toolbar);
	edje_object_part_swallow(edje, "toolbar", ad->toolbar);


	evas_object_show(ad->bt1);
	evas_object_show(ad->bt2);

	evas_object_show(ad->button);



}

int status_setting(void *data){
	appdata* ad = (appdata*) data;
	int x,y,w,h;

	//resize first
	evas_object_geometry_get(ad->win_main, &x, &y, &w, &h);

	evas_object_resize(ad->win_main,500, 500);


	ad->layout_main = libabs_appfwk_load_edj(ad->win_main, "./status-list.edj", "state-list");
	evas_object_resize(ad->layout_main,400, 300);

	evas_object_geometry_get(ad->layout_main, &x, &y, &w, &h);

	ad->li=elm_list_add(ad->win_main);


	evas_object_show(ad->li);

	Evas_Object* ic = elm_icon_add(ad->li);
	elm_icon_standard_set(ic, "arrow_left");

	int length= character_length(ad->char_db_object);
	int id = 1;

	for(;id <= length;id++){
		GString* ins = character_get_name(ad->char_db_object, id);
		struct message * mes = (struct message *) malloc(sizeof(message));
		mes->index = id;
		mes->ad = ad;
		elm_list_item_append(ad->li, ins->str, ic, NULL, clicked, mes);
		g_string_free(ins, TRUE);
	}

	evas_object_show(ic);

	elm_list_go(ad->li);
	evas_object_size_hint_weight_set(ad->li, 1.0, 1.0);
	evas_object_size_hint_align_set(ad->li, -1.0,-1.0);
	evas_object_show(ad->li);


	evas_object_resize(ad->li, 400, 100);

	Evas_Object* edje = elm_layout_edje_get(ad->layout_main);

	edje_object_part_swallow(edje, "list", ad->li);
	evas_object_resize(ad->li, 400, 100);

	evas_object_show(ad->layout_main);
	evas_object_show(ad->win_main);





	ad->button = elm_box_add(ad->win_main);
	elm_box_horizontal_set(ad->button, 1);
	elm_box_homogenous_set(ad->button, 1);
	evas_object_show(ad->button);


	ad->bt1=elm_button_add(ad->win_main);
	elm_button_label_set(ad->bt1, "ok");
	evas_object_size_hint_weight_set(ad->bt1, 1.0, 1.0);
	evas_object_size_hint_align_set(ad->bt1, -1.0,-1.0);
	elm_box_pack_end(ad->button, ad->bt1);
	evas_object_show(ad->bt1);
	evas_object_smart_callback_add(ad->bt1, "clicked", bt_ok, ad);

	ad->bt2=elm_button_add(ad->win_main);
	elm_button_label_set(ad->bt2, "cancel");
	evas_object_size_hint_weight_set(ad->bt2, 1.0, 1.0);
	evas_object_size_hint_align_set(ad->bt2, -1.0,-1.0);
	elm_box_pack_end(ad->button, ad->bt2);
	evas_object_show(ad->bt2);

	edje = elm_layout_edje_get(ad->layout_main);
	evas_object_smart_callback_add(ad->bt2, "clicked", bt_cancel, ad);

	edje_object_part_swallow(edje, "button", ad->button);
	evas_object_resize(ad->button, 400, 50);
	evas_object_hide(ad->bt1);
	evas_object_hide(ad->bt2);

	evas_object_hide(ad->button);


	return 0;
}

EAPI int 
elm_main(int argc, char** argv){
	appdata ad;

	libabs_appfwk_init();

	ad.win_main = libabs_appfwk_get_main_window("status");
	evas_object_smart_callback_add(ad.win_main, "delete,request", win_del, NULL);

	Evas_Object* bg = elm_bg_add(ad.win_main);
	evas_object_size_hint_weight_set(bg, 1.0, 1.0);
	elm_win_resize_object_add(ad.win_main, bg);
	evas_object_show(bg);

	ad.char_db_object = libcal_create_object();

	ecore_idler_add(status_setting, &ad);

	libabs_appfwk_start_loop();
	libcal_destroy_object(ad.char_db_object);

	libabs_appfwk_quit();
}
ELM_MAIN()
